package com.stephenone.zhdaily.homepage;

import android.support.annotation.NonNull;
import android.util.Log;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

import com.stephenone.zhdaily.data.api.ZhiHuDailyApiHelper;
import com.stephenone.zhdaily.data.bean.News;
import com.stephenone.zhdaily.data.source.NewsRepository;
import com.stephenone.zhdaily.uitl.schedulers.BaseSchedulerProvider;

import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;


/**
 * Created by Administrator on 2018/1/2.
 */

public class HomepagePresenter implements HomepageContract.Presenter {
    private static final String TAG = HomepagePresenter.class.getSimpleName();
    private HomepageContract.View mHomepageView;
    private CompositeDisposable mCompositeDisposable;
    private NewsRepository mNewsRepository;
    private BaseSchedulerProvider mSchedulerProvider;
    private boolean mFirstLoad = true;

    public HomepagePresenter(@NonNull NewsRepository newsRepository,
                             @NonNull HomepageContract.View homepageView,
                             @NonNull BaseSchedulerProvider schedulerProvider){
        mHomepageView = checkNotNull(homepageView);
        mNewsRepository = checkNotNull(newsRepository);
        mSchedulerProvider = checkNotNull(schedulerProvider);
        mCompositeDisposable = new CompositeDisposable();
        mHomepageView.setPresenter(this);
    }

    @Override
    public void subscribe() {
//        getHomepageData();
        Log.e(TAG, "subscribe: ");
        loadNews(false);
    }

    @Override
    public void loadNews(boolean forceUpdate) {
        Log.e(TAG, "loadLatestNews: forceUpdate" + forceUpdate  );
        loadNews(forceUpdate || mFirstLoad,true);
        mFirstLoad = false;
    }

    private void loadNews(boolean forceUpdate, boolean showLoadingUI) {
        if (showLoadingUI) {
            mHomepageView.setLoadingIndicator(true);
        }
        if (forceUpdate) {
            mNewsRepository.refreshNews();
        }
        // TODO: 2018/3/7
        mHomepageView.setLoading(true);

        mCompositeDisposable.clear();
        mCompositeDisposable.add(mNewsRepository.getNews()
                .subscribeOn(mSchedulerProvider.io())
                .observeOn(mSchedulerProvider.ui())
                .subscribe(
                        // onNext
                        result -> {
                            showNews(result);
                            mHomepageView.setLoading(false);
                        },
                        // onError
                        throwable -> {
                            Log.e(TAG, "loadLatestNews: " + throwable.getMessage() );
                        },
                        // onCompleted
                        () -> mHomepageView.setLoadingIndicator(false)));
    }

    @Override
    public void unsubscribe() {
        mCompositeDisposable.clear();
    }



    private void showNews(List<News> news){
        mHomepageView.showNews(news);
    }

    @Override
    public void loadMore(String current) {
        Log.e(TAG, "loadMore: current " + current );
        if (mHomepageView.isLoading()) {
            return ;
        }
        mHomepageView.setLoading(true);

//        mCompositeDisposable.add(ZhiHuDailyApiHelper.sService.getMoreNews(current)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(result -> {
//                    showMoreNews(result);
//                    mHomepageView.setLoading(false);
//                }));

        mCompositeDisposable.add(mNewsRepository.getOneNews(current)
                .filter(newsOptional -> newsOptional.isPresent())
                .map(newsOptional -> newsOptional.get())
                .subscribeOn(mSchedulerProvider.io())
                .observeOn(mSchedulerProvider.ui())
                .subscribe(
                        // onNext
                        result -> {
                            showMoreNews(result);
                            mHomepageView.setLoading(false);
                        }
//                        ,
//                        // onError
//                        throwable -> {
//                        },
//                        // onCompleted
//                        () -> mHomepageView.setLoadingIndicator(false)
                ));

    }

    private void showMoreNews(News news){
        mHomepageView.showMoreNews(news);
    }
}
